
package org.amos.upms.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckSafe;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.amos.core.basic.annotation.Log;
import org.amos.core.basic.annotation.ParamCheck;
import org.amos.core.basic.base.BaseController;
import org.amos.core.basic.utils.AmosUtils;
import org.amos.core.basic.vo.R;
import org.amos.upms.modules.system.dto.DeptDTO;
import org.amos.upms.modules.system.entity.Dept;
import org.amos.upms.modules.system.service.IDeptService;
import org.amos.upms.modules.system.vo.DeptTreeVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 部门 前端控制器
 *
 * @author liubt
 * @since 2020-12-20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    private final IDeptService deptService;

    @GetMapping("/list")
    @Operation(description = "部门列表信息")
    @SaCheckPermission("dept:list")
    @Log(value = "部门列表信息", logResultInfo = false)
    public R list(DeptDTO dto) {
        List<DeptTreeVO> voList = deptService.selectTree(dto);
        return R.ok(voList);
    }

    @PostMapping("/update")
    @Operation(description = "更新部门数据")
    @SaCheckPermission("dept:update")
    @Log(value = "更新部门数据", logResultInfo = false)
    public R update(@RequestBody @Validated DeptDTO dto) {
        Dept dept = AmosUtils.copy(dto, Dept.class);
        deptService.saveOrUpdateDept(dept);
        return R.ok();
    }

    @PostMapping("/remove")
    @Operation(description = "删除部门数据")
    @ParamCheck(message = "缺少必要参数")
    @SaCheckPermission("dept:update")
    @SaCheckSafe
    @Log(value = "删除部门数据", logResultInfo = false)
    public R remove(@RequestBody Set<Long> ids) {
        deptService.remove(ids);
        return R.ok();
    }
}
